<?php
    include_once('../src/classes/class.mssql.php');
    
    session_start();
    
    $comtec_id_machine = $_GET["comtec_id_machine"];
    $session = isset($_SESSION["users"])? $_SESSION["users"]:"";
    
    $db = new MSSQL();
    $tsql =  "SELECT comtec_id_operation as Codigo, operation_type as Tipo,brand as Marca, ";
    $tsql .= "(r.name+' '+r.lastname1+' '+r.lastname2) as Encargado, ow.company_name as Propietario,";
    $tsql .= "CONVERT (varchar,o.operation_date_end,103) as Fecha,";
    $tsql .= " m.cod_mainteiner,m.model,o.revised";
    $tsql .= " FROM ((([dbo].[operation] as o INNER JOIN [dbo].[responsible] as r ";
    $tsql .= " ON o.comtec_id_responsible = r.comtec_id_responsible)";
    $tsql .= " INNER JOIN [dbo].[machines] as m ON o.comtec_id_machine = m.comtec_id_machine)) ";
    $tsql .= " INNER JOIN [dbo].[owner] as ow ON m.comtec_id_owner = ow.comtec_id_owner";  
    $tsql .= " WHERE o.comtec_id_machine=$comtec_id_machine and o.status_reg=1
               ORDER BY o.operation_date_end DESC";
    
    $stmt = $db->query($tsql);       
    
     $texto = "<table class='table'>
              <tr>";
    
    if($_SESSION["users"] == "managers"){
     $texto .= "<th colspan=2></th>";
    }else{ if($_SESSION["users"] != "clients") { $texto .= "<th></th>";}}            
    
    $texto .= "<th>C&oacute;digo</th>
              <th>Tipo</th>
              <th>C&oacute;d. m&aacute;quina</th>
              <th>Modelo</th>
              <th>Marca</th>
              <th>Propietario</th>
              <th>Encargado</th>";
    $texto .= "<th>Fecha oper.</th>";
    if(strcmp($_SESSION["users"],"clients")!=0){
     $texto .= "<th>Revisado</th>";
    }
    $texto .= "</tr>";  
    $cont = 0;
    
    while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
     $cont += 1;    
     $Codigo=$row['Codigo'];   
     $Tipo=$row['Tipo'];
     $Marca=$row['Marca'];
     $Encargado=$row['Encargado'];
     $Propietario=$row['Propietario'];
     $Fecha=$row['Fecha'];
     $cod_mainteiner=$row['cod_mainteiner'];
     $Modelo=$row['model'];
     $revised=$row['revised'];
     $texto .= "<tr>";
     if($_SESSION["users"]!="clients"){
     $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
     $texto .= "<td><a href='javascript:loadOperationMachine(".$Codigo.",".$comtec_id_machine.");'><img src='$url_edit' alt='editar' style='border: 0px;' /></a></td>";
     }if($_SESSION["users"]=="managers"){$url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
     $texto .= "<td><a href='javascript:deleteOperation(".$Codigo.",".$comtec_id_machine.");'><img src='$url_delete' alt='eliminar' style='border: 0px;' /></a></td>";}
     $texto .= "<td><a href='javascript:operation(".$Codigo.",".$comtec_id_machine.");'>$Codigo</a></td>";
     switch ($Tipo) {
      case 1: 
             $Tipo = "Mantenci&oacute;n";
      break;
      case 2: 
             $Tipo = "Emergencia";
      break; 
      case 3: 
             $Tipo = "Instalaci&oacute;n";
      break;
      case 4:
             $Tipo = "Reparaci&oacute;n";
      break;    
     }
     $texto .= "<td>$Tipo</td>";
     $texto .= "<td>$cod_mainteiner</td>";
     $texto .= "<td>$Modelo</td>";
     $texto .= "<td>$Marca</td>";
     $texto .= "<td>$Propietario</td>";
     $texto .= "<td>$Encargado</td>";   
     $texto .= "<td>$Fecha</td>";
     switch ($revised) {
         case 0: $revised = "No";
         break;
         case 1: $revised = "Si";
         break;
     }
     if($_SESSION["users"]!="clients"){
     $texto .= "<td>$revised</td>";}
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
    echo "<br /><input type='button' value='Regresar' onclick='javascript:loadHome()' /><br />";
?>
